﻿using Ninject;
using Romantics.Domain;
using Romantics.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Romantics.Controllers
{
    public class ArchiveController : Controller
    {
        private readonly IDomainRepository _domainRepository;

        public ArchiveController(IDomainRepository domainRepository)
        {
            _domainRepository = domainRepository;
        }

        //
        // GET: /Hike/

        public ViewResult Index(int? year, int page)
        {
            // pick latest 10 posts
            var listViewModel = HikeViewModel.Archive(_domainRepository, year, page);
            if (!year.HasValue)
                ViewBag.Title = "Архивы походов";
            else
                ViewBag.Title = String.Format("{0} год. Архивы походов", year);
            return View("_List", listViewModel);
        }

        public ViewResult Category(int? year, string category_url = "", int page = 1)
        {
            var category = _domainRepository.Category(category_url);
            var listViewModel = HikeViewModel.Archive(_domainRepository, year, page, category);
            if (!year.HasValue)
            {
                if(category == null)
                    ViewBag.Title = "Архивы походов";
                else
                    ViewBag.Title = String.Format("Архивы походов в категории \"{0}\"", category.Name);
            }
            else
            {
                if (category == null)
                    ViewBag.Title = String.Format("{0} год. Архивы походов", year);
                else
                    ViewBag.Title = String.Format("{0} год. Архивы походов в категории \"{1}\"", year, category.Name);
            }
            return View("_List", listViewModel);
        }

        public ViewResult Region(int? year, string region_url = "", int page = 1)
        {
            var region = _domainRepository.Region(region_url);
            var listViewModel = HikeViewModel.Archive(_domainRepository, year, page, null, region);
            if (!year.HasValue)
            {
                if (region == null)
                    ViewBag.Title = "Архивы походов";
                else
                    ViewBag.Title = String.Format("Архивы походов в регионе \"{0}\"", region.Name);
            }
            else
            {
                if (region == null)
                    ViewBag.Title = String.Format("{0} год. Архивы походов", year);
                else
                    ViewBag.Title = String.Format("{0} год. Архивы походов в регионе \"{1}. {2}\"", year, region.Country.Name, region.Name);
            }
            return View("_List", listViewModel);
        }

        public ViewResult Report(int id)
        {
            var report = _domainRepository.Report(id);

            if (report == null || !report.Published || report.Hike == null || !report.Hike.Published)
                throw new HttpException(404, "Post not found");

            report.Options = _domainRepository.Options();

            return View(report);
        }
        /// <summary>
        /// Child action that returns the sidebar partial view.
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        public PartialViewResult Sidebars(string action)
        {
            var widgetViewModel = new WidgetViewModel(_domainRepository, action);
            if (RouteData.Values.ContainsKey("category_url"))
            {
                ViewBag.Category = _domainRepository.Category(RouteData.Values["category_url"].ToString());
            }
            if (RouteData.Values.ContainsKey("region_url"))
            {
                ViewBag.Region = _domainRepository.Region(RouteData.Values["region_url"].ToString());
            }
            if (RouteData.Values.ContainsKey("year"))
            {
                ViewBag.Year = RouteData.Values["year"].ToString();
            }
            return PartialView("_Sidebars", widgetViewModel);
        }
    }
}
